//
//  BYBMenuView.m
//  Physician
//
//  Created by xxx on 14-9-12.
//  Copyright (c) 2014年 wtf. All rights reserved.
//

#import "BYBMenuView.h"

@interface BYBMenuView()

@end

@implementation BYBMenuView
{
//    BOOL _hideToPoint;
    
    UIControl   *_overlayView;
    
//    CGPoint _pointToHide;
    
    CGRect _lineToHide;
    
    CGRect _originFrame;
}

//- (id)initWithFrame:(CGRect)frame pointToHide:(CGPoint)pointToHide
//{
//    _pointToHide = pointToHide;
//    CGRect frameInit;
//    frameInit.origin = _pointToHide;
//    frameInit.size = CGSizeZero;
//    if (self = [super initWithFrame:frameInit]) {
//        _hideToPoint = YES;
//        _originFrame = frame;
////        self.separatorStyle = UITableViewCellSeparatorStyleNone;
//    }
//    return self;
//}

- (id)initWithFrame:(CGRect)frame lineToHide:(CGRect)line
{
    _lineToHide = line;
    if (self = [super initWithFrame:line])
    {
//        _hideToPoint = NO;
        _originFrame = frame;
        _overlayView = [[UIControl alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        _overlayView.backgroundColor = [UIColor colorWithRed:.16 green:.17 blue:.21 alpha:.5];
        [_overlayView addTarget:self
                         action:@selector(dismiss)
               forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

//- (id)initWithFrame:(CGRect)frame
//{
//    _pointToHide = CGPointMake(frame.origin.x + frame.size.width, frame.origin.y);
//    CGRect frameInit;
//    frameInit.origin = _pointToHide;
//    frameInit.size = CGSizeZero;
//    if (self = [super initWithFrame:frameInit]) {
//        _originFrame = frame;
//        self.separatorStyle = UITableViewCellSeparatorStyleNone;
//    }
//    return self;
//}

//- (void)showWithAdditionalAnimations:(void (^)(void))bybMenuShowBlock
//{
//    if (!_folded) return;
//    _folded = NO;
//    
//    if (_hideToPoint)
//    {
//        [UIView animateWithDuration:0.4
//                              delay:0.0
//             usingSpringWithDamping:1.0
//              initialSpringVelocity:4.0
//                            options: UIViewAnimationOptionCurveEaseInOut
//                         animations:^{
//                             bybMenuShowBlock();
//                             self.hidden = NO;
//                             [self.superview bringSubviewToFront:self];
////                             CGRect frame = self.frame;
////                             frame.origin.x = _originFrame.origin.x;
////                             frame.origin.y = _originFrame.origin.y;
////                             frame.size.width = _originFrame.size.width;
////                             frame.size.height = _originFrame.size.height;
//                             self.frame = _originFrame;
//                         }
//                         completion:nil];
//    }
//    else
//    {
//        [UIView animateWithDuration:0.4
//                              delay:0.0
//             usingSpringWithDamping:1.0
//              initialSpringVelocity:4.0
//                            options: UIViewAnimationOptionCurveEaseInOut
//                         animations:^{
//                             bybMenuShowBlock();
//                             self.hidden = NO;
//                             [self.superview bringSubviewToFront:self];
////                             CGRect frame = self.frame;
////                             frame.origin.x = _originFrame.origin.x;
////                             frame.origin.y = _originFrame.origin.y;
////                             frame.size.width = _originFrame.size.width;
////                             frame.size.height = _originFrame.size.height;
//                             self.frame = _originFrame;
//                         }
//                         completion:nil];
//    }
//}
//
//- (void)hideWithAdditionalAnimations:(void (^)(void))bybMenuHideBlock completion:(void (^)(BOOL finished))bybMenuHideComletion
//{
//    if (_folded) return;
//    _folded = YES;
//    
//    if (_hideToPoint)
//    {
//        [UIView animateWithDuration:0.2f
//                              delay:0.05f
//             usingSpringWithDamping:1.0
//              initialSpringVelocity:4.0
//                            options: UIViewAnimationOptionCurveEaseInOut
//                         animations:^{
//                             bybMenuHideBlock();
//                             CGRect frame = self.frame;
//                             
//                             frame.origin.x = _pointToHide.x;
//                             frame.origin.y = _pointToHide.y;
//                             frame.size.width = 1;
//                             frame.size.height = 1;
//                             self.frame = frame;
//                         }
//                         completion:^(BOOL finished){
//                             self.hidden = YES;
//                             if (bybMenuHideComletion) bybMenuHideComletion(finished);
//                         }];
//    }
//    else
//    {
//        [UIView animateWithDuration:0.2f
//                              delay:0.05f
//             usingSpringWithDamping:1.0
//              initialSpringVelocity:4.0
//                            options: UIViewAnimationOptionCurveEaseInOut
//                         animations:^{
//                             bybMenuHideBlock();
////                             CGRect frame = self.frame;
////                             
////                             frame.origin.x = _lineToHide.origin.x;
////                             frame.origin.y = _lineToHide.origin.y;
////                             frame.size.width = _lineToHide.size.width;
////                             frame.size.height = _lineToHide.size.height;
//                             self.frame = _lineToHide;
//                         }
//                         completion:^(BOOL finished){
//                             self.hidden = YES;
//                             if (bybMenuHideComletion) bybMenuHideComletion(finished);
//                         }];
//    }
//}

- (void)fadeIn
{
    self.alpha = 0;
    self.hidden = NO;
    self.frame = _lineToHide;
    [UIView animateWithDuration:.35 animations:^{
        self.alpha = 1;
        self.frame = _originFrame;
    }];
    
}
- (void)fadeOut
{
    [UIView animateWithDuration:.35 animations:^{
        self.frame = _lineToHide;
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        if (finished) {
            self.hidden = YES;
            [_overlayView removeFromSuperview];
            [self removeFromSuperview];
        }
    }];
}

- (void)toggle
{
    _folded ? [self show] : [self dismiss];
}

- (void)show
{
    UIWindow *keywindow = [[UIApplication sharedApplication] keyWindow];
    [keywindow addSubview:_overlayView];
    [keywindow addSubview:self];
    [self fadeIn];
    _folded = NO;
}

- (void)dismiss
{
    [self fadeOut];
    _folded = YES;
}

@end
